import Vue from 'vue';
import Router from 'vue-router';
import { AuthGuard } from '@/utils';
import qs from 'qs';
import { globalRoutes, indexRoutes} from '@/router';
import { AppServiceBase } from 'ibiz-core';
import { AppNavHistory, AppViewShell,AppIndexViewShell } from 'ibiz-vue';
Vue.use(Router);
<#macro getViewCaptionTag view><#if view.getCapPSLanguageRes?? && view.getCapPSLanguageRes()??>${view.getCapPSLanguageRes().getLanResTag()}</#if></#macro>
<#assign appIndexView = view />

const router = new Router({
    routes: [
        <#--  路由内容  -->
        {
            path: '/${appIndexView.getCodeName()?lower_case}/:${appIndexView.getCodeName()?lower_case}?',
            beforeEnter: (to: any, from: any, next: any) => {
                const routerParamsName = '${appIndexView.getCodeName()?lower_case}';
                const params: any = {};
                if (to.params && to.params[routerParamsName]) {
                    Object.assign(params, qs.parse(to.params[routerParamsName], { delimiter: ';' }));
                }
                const url: string = '/appdata';
                const auth: Promise<any> = AuthGuard.getInstance().authGuard(url, params, router);
                auth.then((result) => {
                    next(result ? true : false);
                }).catch(() => {
                    next(false);
                });
            },
            meta: {  
                captionTag: '<@getViewCaptionTag view = appIndexView />',
                caption: '${appIndexView.getCaption()}',
                info:'',
                viewType: 'APPINDEX',
                dynaModelFilePath:'${appIndexView.getDynaModelFilePath()}',
                <#if appIndexView.getPSSysImage()??>
                imgPath: '${appIndexView.getPSSysImage().getImagePath()}',
                iconCls: '${appIndexView.getPSSysImage().getCssClass()}',
                </#if>
                parameters: [
                    { pathName: 'views', parameterName: '${appIndexView.getCodeName()?lower_case}' },
                ],
                requireAuth: <#if appIndexView.getAccUserMode?? && appIndexView.getAccUserMode()?? && (appIndexView.getAccUserMode() == 0 || appIndexView.getAccUserMode() == 3)>false<#else>true</#if>,
            },
            component: AppIndexViewShell,
            children: [
            <#if app.getAllPSAppDataEntities?? && app.getAllPSAppDataEntities()??>
                <#list app.getAllPSAppDataEntities() as appDataEntity>
                    <#if appDataEntity.getPSAppDERSPathCount() gt 0>
                    <#--  BEGIN：有多重路径  -->
                    <#list 1..appDataEntity.getPSAppDERSPathCount() as count>
                        <#assign path = ''/>
                        <#--  BEGIN：循环路径下所有关系  -->
                        <#list appDataEntity.getPSAppDERSPath(count_index) as deRSPath>
                        <#assign majorPSAppDataEntity = deRSPath.getMajorPSAppDataEntity()/>
                        <#assign path>${path}${srfpluralize(majorPSAppDataEntity.codeName)?lower_case}/:${majorPSAppDataEntity.getCodeName()?lower_case}?/</#assign>
                        </#list>
                        <#--  END：循环路径下所有关系  -->
                    {
                    path: '${path}${srfpluralize(appDataEntity.codeName)?lower_case}/:${appDataEntity.getCodeName()?lower_case}?/views/:view?',
                    meta: {
                        captionTag: '',
                        caption: '',
                        info:'',
                        imgPath: '',
                        iconCls: '',
                        parameters: [
                            { pathName: '${appIndexView.getCodeName()?lower_case}', parameterName: '${appIndexView.getCodeName()?lower_case}' },
                            <#list appDataEntity.getPSAppDERSPath(count_index) as deRSPath>
                            <#assign majorPSAppDataEntity = deRSPath.getMajorPSAppDataEntity()/>
                            { pathName: '${srfpluralize(majorPSAppDataEntity.codeName)?lower_case}', parameterName: '${majorPSAppDataEntity.getCodeName()?lower_case}' },
                            </#list>
                            { pathName: '${srfpluralize(appDataEntity.codeName)?lower_case}', parameterName: '${appDataEntity.getCodeName()?lower_case}' },
                            { pathName: 'views', parameterName: 'view' },
                        ],
                        resource:'${appDataEntity.getCodeName()?lower_case}',
                        requireAuth: false,
                        },
                        component: AppViewShell,
                    },
                    </#list>
                    <#--  END：有多重路径  -->
                    </#if>
                    {
                    path: '${srfpluralize(appDataEntity.codeName)?lower_case}/:${appDataEntity.getCodeName()?lower_case}?/views/:view?',
                    meta: {
                        captionTag: '',
                        caption: '',
                        info:'',
                        imgPath: '',
                        iconCls: '',
                        parameters: [
                            { pathName: '${appIndexView.getCodeName()?lower_case}', parameterName: '${appIndexView.getCodeName()?lower_case}' },
                            { pathName: '${srfpluralize(appDataEntity.codeName)?lower_case}', parameterName: '${appDataEntity.getCodeName()?lower_case}' },
                            { pathName: 'views', parameterName: 'view' },
                        ],
                        resource:'${appDataEntity.getCodeName()?lower_case}',
                        requireAuth: false,
                    },
                    component: AppViewShell
                    },
                </#list>
            </#if>
            <#-- 应用视图start -->
            {
                path: 'views/:view?',
                meta: {
                    captionTag: '',
                    caption: '',
                    info:'',
                    imgPath: '',
                    iconCls: '',
                    parameters: [
                        { pathName: '${appIndexView.getCodeName()?lower_case}', parameterName: '${appIndexView.getCodeName()?lower_case}' },
                        { pathName: 'views', parameterName: 'view' },
                    ],
                    requireAuth: false,
                },
                component: AppViewShell,
            },
            <#-- 应用视图end -->
            {
                path: '/appredirectview',
                name: 'appredirectview',
                meta: {
                    captionTag: '',  
                    caption: '全局应用重定向视图',
                    viewType: 'REDIRECTVIEW',
                    requireAuth: false,
                    ignoreAddPage: true,
                },
                component: () => import('@components/app-redirect-view/app-redirect-view.vue'),
            },
            ...indexRoutes,
            ],
        },
        ...globalRoutes,
        {
            path: '/login/:login?',
            name: 'login',
            meta: {
                captionTag: '',
                caption: '登录',
                viewType: 'login',
                requireAuth: false,
                ignoreAddPage: true,
            },
            beforeEnter: (to: any, from: any, next: any) => {
                router.app.$store.commit('resetRootStateData');
                next();
            },
            component: () => import('@components/login/login.vue'),
        },
        {
            path: '/lock',
            name: 'lock',
            meta: {  
                captionTag: '',
                caption: '锁屏',
                viewType: 'lock',
                requireAuth: false,
                ignoreAddPage: true,
            },
            component: () => import('@components/app-lock/app-lock.vue'),
        },
        {
            path: '/appredirectview',
            name: 'appredirectview',
            meta: {
                captionTag: '',  
                caption: '全局应用重定向视图',
                viewType: 'REDIRECTVIEW',
                requireAuth: false,
                ignoreAddPage: true,
            },
            component: () => import('@components/app-redirect-view/app-redirect-view.vue'),
        },
        {
            path: '/404',
            component: () => import('@components/404/404.vue')
        },
        {
            path: '/500',
            component: () => import('@components/500/500.vue')
        },
        {
            path: '/appsharepage',
            meta: {
                caption: '应用分享页',
                requireAuth: true,
                ignoreAddPage: true
            },
            component: () => import('@components/app-share-page/app-share-page.vue')
        },
        {
            path: '*',
            redirect: '${appIndexView.getCodeName()?lower_case}'
        },
    ],
});

// 解决路由跳转路由重复时报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location: any) {
    let result: any = originalPush.call(this, location);
    return result.catch((err: any) => err);
}
export default router;